package cn.tedu._06article.controller;

import cn.tedu._06article.base.response.JsonResult;
import cn.tedu._06article.base.response.StatusCode;
import cn.tedu._06article.mapper.UserMapper;
import cn.tedu._06article.pojo.dto.UserArticleParam;
import cn.tedu._06article.pojo.dto.UserLoginParam;
import cn.tedu._06article.pojo.entity.Article;
import cn.tedu._06article.pojo.entity.User;
import cn.tedu._06article.pojo.vo.ArticleVO;
import cn.tedu._06article.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@EnableKnife4j
@Validated
@Tag(name = "01-用户管理模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession  session) {
        log.debug("开始处理用户登录功能");
        log.debug("参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByNameAndPassword(userLoginParam);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        log.debug("用户信息：{}", userVO);
        session.setAttribute("user", userVO);
        return JsonResult.ok(userVO);
    }

    @PostMapping("article")
    @Operation(summary = "用户发布文章")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@RequestBody @Validated UserArticleParam  userArticleParam, HttpSession session) {
        log.debug("开始处理用户发布文章功能");
        log.debug("参数：{}",userArticleParam );
        UserVO userVO = (UserVO) session.getAttribute("user");
        Article article = new Article();
        article.setUserId(userVO.getId());
        BeanUtils.copyProperties(userArticleParam,article);
        int rows = userMapper.insertArticle(article);
        return rows > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("selectByArticleId")
    @Operation(summary = "查询文章")
    @ApiOperationSupport(order = 30)
    @Parameter( name = "id", description = "文章编号")
    public JsonResult selectByArticleId(@Range(min = 1,max = 100,message = "文章编号必须在1-100之间") Long id) {
        log.debug("开始处理查询文章功能");
        log.debug("参数：{}",id);
        ArticleVO articleVO = userMapper.selectByArticleId(id);
        return JsonResult.ok(articleVO);
    }
}
